using admin.application.Services;
using admin.core.Entities.App;
using admin.application.Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

namespace admin.api.Controllers;

[Authorize]
[ApiController]
[Route("api/[controller]")]
public class RoleController : ControllerBase
{
    private readonly IAppRoleServices _roleService;
    public RoleController(IAppRoleServices roleService)
    {
        _roleService = roleService;
    }

    [HttpPost]
    public async Task<IActionResult> Create([FromBody] AppRole role)
    {
        var result = await _roleService.CreateRoleAsync(role);
        return Ok(ResponseResult<AppRole>.Ok(result, "创建角色成功"));
    }

    [HttpPut("{id}")]
    public async Task<IActionResult> Update(Guid id, [FromBody] AppRole role)
    {
        var result = await _roleService.UpdateRoleAsync(id, role);
        return Ok(ResponseResult<AppRole?>.Ok(result, "修改角色成功"));
    }

    [HttpDelete("{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        await _roleService.DeleteRoleAsync(id);
        return Ok(ResponseResult<object>.Ok(null, "删除角色成功"));
    }

    [HttpPost("{id}/enable")]
    public async Task<IActionResult> Enable(Guid id)
    {
        try
        {
            await _roleService.EnableRoleAsync(id);
            return Ok(ResponseResult<object>.Ok(null, "启用角色成功"));
        }
        catch (Exception ex)
        {
            return BadRequest(ResponseResult<object>.Fail(ex.Message, 400));
        }
    }

    [HttpPost("{id}/disable")]
    public async Task<IActionResult> Disable(Guid id)
    {
        try
        {
            await _roleService.DisableRoleAsync(id);
            return Ok(ResponseResult<object>.Ok(null, "禁用角色成功"));
        }
        catch (Exception ex)
        {
            return BadRequest(ResponseResult<object>.Fail(ex.Message, 400));
        }
    }

    [HttpPost("{id}/assign-permission")]
    public async Task<IActionResult> AssignPermission(Guid id, [FromQuery] Guid permissionId)
    {
        try
        {
            await _roleService.AssignPermissionAsync(id, permissionId);
            return Ok(ResponseResult<object>.Ok(null, "分配权限成功"));
        }
        catch (Exception ex)
        {
            return BadRequest(ResponseResult<object>.Fail(ex.Message, 400));
        }
    }

    [HttpPost("{id}/remove-permission")]
    public async Task<IActionResult> RemovePermission(Guid id, [FromQuery] Guid permissionId)
    {
        try
        {
            await _roleService.RemovePermissionAsync(id, permissionId);
            return Ok(ResponseResult<object>.Ok(null, "移除权限成功"));
        }
        catch (Exception ex)
        {
            return BadRequest(ResponseResult<object>.Fail(ex.Message, 400));
        }
    }

    [HttpGet]
    public async Task<IActionResult> GetAll()
    {
        var result = await _roleService.GetAllRolesAsync();
        return Ok(ResponseResult<List<AppRole>>.Ok(result, "获取角色列表成功"));
    }

    [HttpGet("{id}/permissions")]
    public async Task<IActionResult> GetRolePermissions(Guid id)
    {
        var permissions = await _roleService.GetRolePermissionsAsync(id);
        return Ok(ResponseResult<List<AppPermission>>.Ok(permissions, "获取角色权限成功"));
    }
} 